package com.thqmcp.server.controller;

import com.thqmcp.server.model.McpMessage;
import com.thqmcp.server.model.McpTool;
import com.thqmcp.server.service.McpService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * MCP服务器控制器
 */
@RestController
@RequestMapping("/mcp")
@RequiredArgsConstructor
@Slf4j
public class McpController {

    private final McpService mcpService;

    /**
     * 获取服务器信息
     */
    @GetMapping("/info")
    public ResponseEntity<McpMessage> getServerInfo() {
        log.info("收到服务器信息请求");
        McpMessage response = mcpService.getServerInfo();
        return ResponseEntity.ok(response);
    }

    /**
     * 获取可用工具列表
     */
    @GetMapping("/tools")
    public ResponseEntity<List<McpTool>> getTools() {
        log.info("收到工具列表请求");
        List<McpTool> tools = mcpService.getAvailableTools();
        return ResponseEntity.ok(tools);
    }

    /**
     * 处理工具调用请求
     */
    @PostMapping("/tools/call")
    public ResponseEntity<McpMessage> callTool(@RequestBody McpMessage request) {
        log.info("收到工具调用请求: {}", request.getMethod());
        McpMessage response = mcpService.callTool(request);
        return ResponseEntity.ok(response);
    }

    /**
     * 处理通用MCP消息
     */
    @PostMapping("/message")
    public ResponseEntity<McpMessage> handleMessage(@RequestBody McpMessage message) {
        log.info("收到MCP消息: type={}, method={}", message.getType(), message.getMethod());
        McpMessage response = mcpService.handleMessage(message);
        return ResponseEntity.ok(response);
    }
} 